//  ReceiveController.m
//
//  Youhuiju
//
//  Created by liuovyue on 16/8/12.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "ReceiveController.h"
#import "CellNewAddress.h"
#import "NewAddressTwoController.h"
#import "ModelNewAddress.h"
#import "ModelAddress.h"

@interface ReceiveController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)UILabel *labelTitle;
@property (nonatomic, strong)UIView *viewBar;
@property (nonatomic, strong)UIButton *buttonReturn;
@property (nonatomic, strong)UIButton *buttonNewAddress;//新建地址
@property (nonatomic, strong)UIView *viewBackground;
@property (nonatomic, strong)UIImageView *imageStations;
@property (nonatomic, strong)UILabel *labelStations;
@property (nonatomic, strong)CellNewAddress *cell;
@property (nonatomic, strong)NSMutableArray *arrIndex;
@property (nonatomic, strong)NSMutableArray *arrNew;
@property (nonatomic, strong)NSMutableArray *arrAddress;
@end

@implementation ReceiveController
- (void)viewWillAppear:(BOOL)animated {
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(passAddress:) name:@"address" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.arrIndex = [NSMutableArray array];
    
    self.arr = @[].mutableCopy;
    self.arrNew = [NSMutableArray array];
    [self createNavigationBar];
    [self createTableView];
    if (self.arr.count ==0) {
        self.view.backgroundColor = [UIColor whiteColor];
        [self createStationsPic];
        self.tableView.alpha = 0;
    }else {
        self.view.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
        self.tableView.alpha = 1;
    }
    [self createButton];
    
   
}

- (void)passAddress:(NSNotification *)text {
   
   
    [self.arr addObject:text.userInfo];
   
    self.view.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
    self.tableView.alpha = 1;
    self.imageStations.alpha = 0;
    self.labelStations.alpha = 0;
   self.arrAddress = [NSMutableArray array];
    
    for (NSDictionary *dicOne in self.arr) {
      
        ModelAddress *model = [[ModelAddress alloc]init];
        [model setValuesForKeysWithDictionary:dicOne];
       
        
        [self.arrAddress addObject:model];
        
    }
  
    [self.tableView reloadData];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"address" object:nil];
}







//当地址管理没有信息时候显示站位图
- (void)createNavigationBar{
    
    self.viewBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.viewBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewBar];
    
    
    self.labelTitle = [[UILabel alloc]init];
    self.labelTitle.text = @"地址管理";
    self.labelTitle.tintColor = [UIColor blackColor];
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    
    self.labelTitle.font = [UIFont systemFontOfSize:18*FitW];
    [self.viewBar addSubview:self.labelTitle];
    
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
      
        make.left.equalTo(self.viewBar.mas_left).offset(100*FitW);
        make.right.equalTo(self.viewBar.mas_right).offset(-100*FitW);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-14*FitH);
        
        
        
    }];
    //返回按钮
    self.buttonReturn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonReturn setImage:[UIImage imageNamed:@"zfanhuijian"] forState:UIControlStateNormal];
    [self.buttonReturn addTarget:self action:@selector(tuihuiAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonReturn];
    
    [_buttonReturn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.viewBar.mas_left).offset(17*FitW);
        make.width.mas_equalTo(22*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
        
        
    }];
    
    
    
}

#pragma mark -- 未收藏的时候显示占位图
- (void)createStationsPic {
    
    self.imageStations = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dz"]];
    
    [self.view addSubview:self.imageStations];
    [self.imageStations mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.viewBar.mas_bottom).offset(202*FitH);
        make.left.equalTo(self.view.mas_left).offset(153*FitW);
        make.height.mas_equalTo(74*FitH);
        make.width.mas_equalTo(89*FitW);
        
        
        
    }];
    
    self.labelStations = [[UILabel alloc]init];
    self.labelStations.text = @"您还没有收货地址哦 !";
    self.labelStations.font = [UIFont systemFontOfSize:18*FitW];
    self.labelStations.textColor = [UIColor colorWithHexString:@"333333"];
    [self.view addSubview:self.labelStations];
    [self.labelStations mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.imageStations.mas_bottom).offset(39*FitH);
        make.left.equalTo(self.view.mas_left).offset(101*FitW);
        
        
    }];
    
}


- (void)tuihuiAcion:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}






- (void)createTableView {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64*FitH, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 127*FitH) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource= self;
    [self.tableView registerNib:[UINib nibWithNibName:@"CellNewAddress" bundle:nil] forCellReuseIdentifier:@"CellNewAddress"];
    self.tableView.separatorStyle =NO;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        
        make.top.equalTo(self.viewBar.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom).offset(-64*FitH);
        
        
    }];
    
    
    
}





- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.arr.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section==0 ) {
        return 0;
    }else {
    return 10*FitH;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"CellNewAddress"];
    [self.cell.buttonDelete addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.arrIndex addObject:indexPath];
    if (self.arrAddress) {
        
        ModelAddress *model = [self.arrAddress objectAtIndex:indexPath.section];
        [self.cell passAddress:model];
    }
    
    return self.cell;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return 150*FitH;
    
}

- (void)deleteAction:(UIButton *)button {
    UIView *v = [button superview];
    UITableViewCell *cell = (UITableViewCell *)[v superview];
    NSIndexPath *indexPathNew = [self.tableView indexPathForCell:cell];
    
    
    [self.arr removeObjectAtIndex:indexPathNew.section];
    if ([self.arr isEqualToArray:@[]]) {
        self.labelStations.alpha = 1;
        self.imageStations.alpha = 1;
    }
    [self.tableView reloadData];
}


- (void)createButton {
    
    
    self.viewBackground = [[UIView alloc]init];
    self.viewBackground.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewBackground];
    
    [self.viewBackground mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.mas_equalTo(65*FitH);
        
        
    }];
    
    self.buttonNewAddress = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonNewAddress.backgroundColor = [UIColor colorWithHexString:@"ff5f7b"];
    [self.buttonNewAddress setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [self.buttonNewAddress setTitle:@"新建地址" forState: UIControlStateNormal];
    [self.buttonNewAddress addTarget:self action:@selector(newAddressAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBackground addSubview:self.buttonNewAddress];
    [self.buttonNewAddress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-18*FitH);
        make.left.equalTo(self.view.mas_left).offset(17*FitW);
        make.width.mas_equalTo(341*FitW);
        make.height.mas_equalTo(45*FitH);
        
    }];
    
}




- (void)newAddressAction:(UIButton *)button {
    
    
    NewAddressTwoController *address = [[NewAddressTwoController alloc]init];
    
    [self.navigationController pushViewController:address animated:YES];
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
    
    for (NSIndexPath *indexPathNew in self.arrIndex) {
        
    
        if (indexPath == indexPathNew ) {
            
        
    CellNewAddress *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.isSelct = 1;
    [cell passButtonSlecter:cell.isSelct];
        }else {
            
            CellNewAddress *cell = [tableView cellForRowAtIndexPath:indexPathNew];
            
            cell.isSelct = 0;
            [cell passButtonSlecter:cell.isSelct];
            
            
            
        }
    
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
